﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace BackupRestoreSoftwares.FormChild
{
    public partial class m_Tools : UserControl
    {
        public m_Tools()
        {
            InitializeComponent();
        }
        string Roaming_path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString();
        string Local_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString();
        string Common_path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).ToString();
        string Program_32 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).ToString();
        string Program_64 = Path.GetPathRoot(Environment.SystemDirectory).ToString() + "Program Files";
        string App_path = Application.StartupPath.ToString();
        bool Is64 = Environment.Is64BitOperatingSystem;

        private void Export(string exportPath, string registryPath)
        {
            string path = "\"" + exportPath + "\"";
            string key = "\"" + registryPath + "\"";
            Process proc = new Process();

            try
            {
                proc.StartInfo.FileName = "regedit.exe";
                proc.StartInfo.UseShellExecute = false;

                proc = Process.Start("regedit.exe", "/e " + path + " " + key + "");
                proc.WaitForExit();
            }
            catch (Exception)
            {
                proc.Dispose();
            }
        }
        private void Import(string registryPath)
        {
            string key = "\"" + registryPath + "\"";
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "regedit.exe";
                proc.StartInfo.UseShellExecute = false;

                proc = Process.Start("regedit.exe", "/s " + key + "");
                proc.WaitForExit();
            }
            catch (Exception)
            {
                proc.Dispose();
            }
        }

        private void btn_bk_idm_Click(object sender, EventArgs e)
        {
            try
            {
                Thread t = new Thread(new ThreadStart(frmMain.waitscreen));
                t.Start();

                string chkfile = Program_32 + @"\Internet Download Manager\IDMan.exe";
                if (File.Exists(chkfile))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "IDMan")
                            app.Kill();
                    Import(App_path + @"\DBBACKUP\IDMSetting.reg");

                    t.Abort();
                    MessageBox.Show("Backup completed!", "Finish");
                }
                else
                {
                    t.Abort();
                    MessageBox.Show("IDM don't install!", "Error");
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }

        private void btn_res_idm_Click(object sender, EventArgs e)
        {
            try
            {
                Thread t = new Thread(new ThreadStart(frmMain.waitscreen));
                t.Start();

                string chkfile = Program_32 + @"\Internet Download Manager\IDMan.exe";
                if (File.Exists(chkfile))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "IDMan")
                            app.Kill();
                    Export(App_path + @"\DBBACKUP\IDMSetting.reg", @"HKEY_CURRENT_USER\Software\DownloadManager");

                    t.Abort();
                    MessageBox.Show("Restore completed!", "Finish");
                }
                else
                {
                    t.Abort();
                    MessageBox.Show("IDM don't install!", "Error");
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }

        private void btn_bk_winrar_Click(object sender, EventArgs e)
        {
            try
            {
                Thread t = new Thread(new ThreadStart(frmMain.waitscreen));
                t.Start();

                if (Is64 == true)
                {
                    string chkfile_64 = Program_64 + @"\WinRAR\WinRAR.exe";
                    string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                    if (File.Exists(chkfile_64) || File.Exists(chkfile_32))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "WinRAR")
                                app.Kill();
                        Export(App_path + @"\WinRARSetting.reg", @"HKEY_CURRENT_USER\Software\WinRAR");

                        t.Abort();
                        MessageBox.Show("Backup completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("WinRAR don't install!", "Error");
                    }
                }
                else
                {
                    string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                    if (File.Exists(chkfile_32))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "WinRAR")
                                app.Kill();
                        Export(App_path + @"\WinRARSetting.reg", @"HKEY_CURRENT_USER\Software\WinRAR");

                        t.Abort();
                        MessageBox.Show("Backup completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("WinRAR don't install!", "Error");
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }

        }

        private void btn_res_winrar_Click(object sender, EventArgs e)
        {
            try
            {
                Thread t = new Thread(new ThreadStart(frmMain.waitscreen));
                t.Start();

                if (Is64 == true)
                {
                    string chkfile_64 = Program_64 + @"\WinRAR\WinRAR.exe";
                    string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                    if (File.Exists(chkfile_64) || File.Exists(chkfile_32))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "WinRAR")
                                app.Kill();
                        Import(App_path + @"\WinRARSetting.reg");

                        t.Abort();
                        MessageBox.Show("Restore completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("WinRAR don't install!", "Error");
                    }
                }
                else
                {
                    string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                    if (File.Exists(chkfile_32))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "WinRAR")
                                app.Kill();
                        Import(App_path + @"\WinRARSetting.reg");

                        t.Abort();
                        MessageBox.Show("Restore completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("WinRAR don't install!", "Error");
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
    }
}
